package com.zhongwang.mms.module.material.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.material.dao.CompositeOutcomeDao;

import com.zhongwang.mms.module.material.model.CompositeOutcomeModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ExportExcelUtils;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.apache.shiro.subject.Subject;

import javax.servlet.http.HttpServletResponse;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 出入库综合查询 服务层
 * @Author: China.zy
 * @Date: 2020/3/21
 */
@Log4j2
@Service
public class CompositeOutcomeService {

    @Autowired
    private CompositeOutcomeDao compositeOutcomeDao;
    @Autowired
    private LogService logService;
    @Value("${user.upload.path}")
    private String uploadPath;

    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 根据条件查询项目完成时间
     *
     * @param compositeOutcomeModel 查询条件
     * @return list
     */
    public List<CompositeOutcomeModel> findOutcome(CompositeOutcomeModel compositeOutcomeModel, List<String> whiIdList) {
        List<CompositeOutcomeModel> list = new ArrayList<>();
        try {
            StringBuilder sb;

            if (whiIdList != null && whiIdList.size() > 0) {
                sb = new StringBuilder();
                for (int i = 0; i < whiIdList.size(); i++) {
                    sb.append(whiIdList.get(i)).append(",");
                }
                compositeOutcomeModel.setWhiIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
            }
            compositeOutcomeModel.setOdaRedFlag(1);
            list = compositeOutcomeDao.findCompositeOutcome(compositeOutcomeModel);
        } catch (Exception e) {
            log.error("原料库出入库综合查询异常：" + e.getMessage());
        }
        return list;
    }

    /**
     * 列表 导出
     *
     * @param param 查询条件
     */
    public JsonResult exportExcel(CompositeOutcomeModel param) {
        JsonResult jsonResult = new JsonResult();
        StringBuilder sb;
        if (param.getWhiIdList() != null && param.getWhiIdList().size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < param.getWhiIdList().size(); i++) {
                sb.append(param.getWhiIdList().get(i)).append(",");
            }
            param.setWhiIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        param.setOdaRedFlag(1); // 不显示红字
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        logService.saveLog("原料库出入库综合查询——导出","用户:【"+user.getName()+"】执行导出操作！");
        //存储/读取Excel的文件夹的路径 mms文件夹下/用户账户/
        String path = uploadPath + user.getAccount() + "/";
        Subject userSubject = ShiroUtils.getSubject();
        String excelFileName = "原料库出入库综合查询.xlsx";
        try {
            //1.创建临时内存表名
            String tempTableName = "##" + user.getAccount() + (UUID.randomUUID().toString().replace("-", ""));
            param.setTempTableName(tempTableName);
            //2.查询出数据导入到临时表
            if (userSubject.isPermitted("matwar:amount:view")) {
                compositeOutcomeDao.createMinQueryTempTableByParam(param);
            } else {
                compositeOutcomeDao.createMinQueryTempTableByParamAndNoAmount(param);
            }
            //3.导出Excel
            if (ExportExcelUtils.BuildExcel(path, excelFileName, tempTableName, active, true)) {
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setData(excelFileName);
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }


}
